package org.biubiu0002.baiblog.core.controller;

import com.google.common.base.Preconditions;
import org.apache.commons.lang3.StringUtils;
import org.biubiu0002.baiblog.common.utils.R;
import org.biubiu0002.baiblog.core.model.entity.SysRoleEntity;
import org.biubiu0002.baiblog.core.model.vo.req.RolePermissionRelationReqVo;
import org.biubiu0002.baiblog.core.model.vo.res.RolePermissionResVo;
import org.biubiu0002.baiblog.core.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;



/**
 * 角色表
 *
 * @author biubiu0002
 * @email 1007918011@qq.com
 * @date 2022-05-06 00:50:38
 */
@RestController
@RequestMapping("/sysrole")
public class SysRoleController {
    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(){
        List<RolePermissionResVo> list= sysRoleService.rolePermissionList();
        return R.ok().put("data", list);
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		SysRoleEntity sysRole = sysRoleService.getById(id);

        return R.ok().put("sysRole", sysRole);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody SysRoleEntity sysRole){
		sysRoleService.save(sysRole);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SysRoleEntity sysRole){
		sysRoleService.updateById(sysRole);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		sysRoleService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    @PutMapping("/{roleId}")
    public R relation(@PathVariable("roleId") Integer roleId,
            @RequestBody RolePermissionRelationReqVo reqVo){
        Preconditions.checkArgument(reqVo.getRoleId()!=null,"角色id不能为空");
        Preconditions.checkArgument(StringUtils.isNotBlank(reqVo.getRoleName()),"角色名称不能为空");
        Preconditions.checkArgument(reqVo.getPermissionIdList()!=null,"资源列表不能为空");
        Preconditions.checkArgument(reqVo.getPermissionIdList().size()<=200,"资源列表容量不能超过200");
        reqVo.setRoleId(roleId);
        sysRoleService.relateRolePermission(reqVo);
        return R.ok();
    }

}
